package com.cqyc.network.http;

import android.text.TextUtils;
import android.util.Log;



import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.nio.charset.StandardCharsets;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okio.Buffer;
import okio.BufferedSource;

/**
 * 异常响应收集, 收集比如 503，500 这类的错误
 *
 * @author cchao
 * @version 2019/4/9.
 */
public class RespLogInterceptor implements Interceptor {
    private final static String TAG = RespLogInterceptor.class.getSimpleName();

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Response response;
        String url = request.url().url().toString();
        try {
            response = chain.proceed(request);

            // 正常响应
            if (response.isSuccessful()) {
                // 获取响应体buffer clone写入
                BufferedSource source = response.body().source();
                source.request(Long.MAX_VALUE);
                Buffer buffer = source.buffer();
                // 复制一份响应
                String json = buffer.clone().readString(StandardCharsets.UTF_8).trim();
                Log.d(TAG + " 收到响应resp", "" + json);
                try {
                    if (TextUtils.isEmpty(json)) {
                        Log.e(TAG +  "返回了空值", "url : " + url);
                    } else {
                        JSONObject jsonObject = new JSONObject(json);
                        if (jsonObject.has("resp_cipher")) {
                            String dataString = jsonObject.getString("resp_cipher");
                            //解密下发的数据
                            String data = AesUtil.decrypt(dataString);
                            //删除原始数据
                            buffer.clear();
                            //写入新数据
                            buffer.write(data.getBytes());
                            //为日志输出提供解密后的数据
                            json = buffer.clone().readString(StandardCharsets.UTF_8).trim();
                        }
                    }
                } catch (JSONException e) {
                    Log.e(TAG + "接口返回不能转成json格式","url : " + url + " json : " + json + " exception " + e.getMessage());
                } catch (Exception e) {
                    Log.e(TAG , e.toString());
                }
                Log.d(TAG + " 最终的resp", "" + json);
                return response;
            }
            // 异常响应
            switch (response.code()) {
                case 500:
                    Log.e(TAG + "响应了500", "url : " + url);
                    break;
                case 404:
                    Log.e(TAG + "响应了404", "url : " + url);
                    break;
                case 503:
                    Log.e(TAG + "响应了503", "url : " + url);
                    break;
                default:
                    Log.e(TAG + "出错的响应", "url : " + url);
                    break;
            }
        } catch (SocketTimeoutException e) {
            Log.e(TAG + "接口请求超时了", "url : " + url);
            throw e;
        }
        return response;
    }
}
